package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.domain.dto.CourseDTO;
import com.example.domain.dto.TimeDTO;
import com.example.domain.po.Chapter;
import com.example.domain.po.Course;
import com.example.domain.vo.CourseContentVo;
import com.example.domain.vo.CourseSum;
import com.example.domain.vo.CourseVo;
import com.example.mapper.CourseMapper;
import com.example.service.CourseService;
import com.example.util.AliOSSUtils;
import com.example.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl implements CourseService {


    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Override
    public PageBean<CourseVo> list(Integer pageNum, Integer pageSize, String name, String department) {
        PageBean<CourseVo> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<CourseVo> departmentVoList = courseMapper.list(name, department);
        System.out.println(departmentVoList);
        departmentVoList.forEach(courseVo -> {
            if (courseVo.getCourseStatus().equals(0)) courseVo.setListed("上架中");
            if (courseVo.getCourseStatus().equals(1)) courseVo.setListed("已下架");
            if (courseVo.getS().equals(0)) courseVo.setType("全部免费");
            if (courseVo.getS().equals(1)) courseVo.setType("会员免费");
            if (courseVo.getS().equals(2)) courseVo.setType("付费");

        });

        Page<CourseVo> p = (Page<CourseVo>) departmentVoList;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void add(CourseDTO courseDTO) throws IOException {
        Course course = BeanUtil.copyProperties(courseDTO, Course.class);
        course.setCount(0);
        course.setStatus(0);
        course.setVideo(aliOSSUtils.upload(courseDTO.getFile()));
        courseMapper.add(course);

    }

    @Override
    public List<Map<String, Object>> listByHeat() {
        return courseMapper.listByHeat();
    }

    @Override
    public List<Map<String, Object>> listByVip(Integer type) {
        return courseMapper.listByVip(type);
    }

    @Override
    public void del(Integer id) {
        courseMapper.del(id);
    }

    @Override
    public void update(CourseDTO courseDTO) {
        Course course = BeanUtil.copyProperties(courseDTO, Course.class);
        course.setName(courseDTO.getCourseName());
        courseMapper.update(course);
    }

    @Override
    public List<Map<String, Object>> listByLike(String name) {
        return courseMapper.listByLike(name);
    }

    @Override
    public PageBean<CourseVo> recommend(Integer pageNum, Integer pageSize, Integer type,Integer userId) {
        PageBean<CourseVo> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<CourseVo> list = courseMapper.recommend(type,userId);
        list.forEach(item->{
            if (item.getCourseStatus().equals(0))   item.setStatus("上架中");
            if (item.getCourseStatus().equals(1))   item.setStatus("已下架");
        });

        Page<CourseVo> p = (Page<CourseVo>) list;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public CourseSum sum(Integer userId) {
        Integer type =2;
        return courseMapper.sum(userId,type);
    }

    @Override
    public CourseSum sumBuy(Integer userId) {

        Integer type =2;
        return courseMapper.sumBuy(userId,type);
    }

    @Override
    public List<CourseContentVo> getCourseContent(Integer courseId, String courseName, String doctorName) {
        var l = courseMapper.getCourseContent(courseId,courseName,doctorName);
        if (l==null) return null;
        for (CourseContentVo vo : l) {
            vo.getChapterList().forEach(item->{
                item.setCourseId(vo.getId());
                item.setFilePOList(courseMapper.finFileByChapterId(item.getCid()));
            });


        }

        return l;
    }

    @Override
    public void addChapter(Chapter chapter) {
        if (chapter.getCid()==null){
            courseMapper.addChapter(chapter);
        }else {
            courseMapper.updateChapter(chapter);
        }

    }

    @Override
    public void update2(Course course) {
        courseMapper.update2(course);
    }

    @Override
    public List<Map<String, Object>> listCFByCid(Integer id) {
        return courseMapper.listCFByCid(id);
    }

    @Override
    public void addTime(TimeDTO timeDTO) {
        if (timeDTO.getFileId()==null){

            courseMapper.addTime(timeDTO);
        }else {
            courseMapper.updateTime(timeDTO);
        }
    }


    @Override
    public void grounding(Integer id) {
        Course course = new Course();
        course.setId(id);
        course.setStatus(0);
        courseMapper.update(course);
    }

    @Override
    public void off(Integer id) {
        Course course = new Course();
        course.setId(id);
        course.setStatus(1);
        courseMapper.update(course);
    }


    @Override
    public Course findById(Integer courseId) {
        return courseMapper.findById(courseId);
    }

    @Override
    public Map<String,Object> getById(Integer id) {
        return courseMapper.getById(id);
    }

}
